package com.pdx.shiro.hashCre;

import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 派 大 星
 * @function
 * @date 2022/4/5 18:44
 * @website https://blog.csdn.net/Gaowumao
 */
public class DigestsUtil {

    private static final String SHA1 = "SHA-1";

    private static final Integer ITERATIONS = 512;

    /**
     * sha1方法
     * @param input 需要散列字符串
     * @param salt 盐字符串
     * @return
     */
    public static String sha1(String input,String salt){
        return new SimpleHash(SHA1,input,salt,ITERATIONS).toString();
    }

    /**
     * 随机获得salt字符串
     * @return
     */
    public static String getSalt(){
        SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
        return generator.nextBytes().toHex();
    }

    /**
     * 生产密码字符密文和salt密文
     * @param passwordPlain
     * @return
     */
    public static Map<String,String> entryptPassword(String passwordPlain){
        Map<String,String> map = new HashMap<>();
        String salt = getSalt();
        String password = sha1(passwordPlain, salt);
        map.put("salt",salt);
        map.put("password",password);
        return map;
    }
}
